#include "dockwindow.h"

DockWindow::DockWindow(QMainWindow *parent) :
        QWidget(parent)
{
    this->resize(parent->width() , parent->height());
    this->setAutoFillBackground(true);
    DockLayout = new QGridLayout(this);
}

DockWindow::~DockWindow()
{
    delete DockLayout;
}

void DockWindow::cpuDock()
{
    exitLabel = new QLabel(this);
    userLabel = new QLabel(this);
    niceLabel = new QLabel(this);
    systemLabel = new QLabel(this);
    waitLabel = new QLabel(this);
    idleLabel = new QLabel(this);
    exitLabel->setPixmap(QPixmap(":/image/detail/cpuExit.png"));
    exitLabel->resize(QPixmap(":/image/detail/cpuExit.png").width() , QPixmap(":/image/detail/cpuExit.png").height());
    userLabel->setPixmap(QPixmap(":/image/detail/cpuUser.png"));
    userLabel->resize(QPixmap(":/image/detail/cpuUser.png").width() , QPixmap(":/image/detail/cpuUser.png").height());
    niceLabel->setPixmap(QPixmap(":/image/detail/cpuNice.png"));
    niceLabel->resize(QPixmap(":/image/detail/cpuNice.png").width() , QPixmap(":/image/detail/cpuNicer.png").height());
    systemLabel->setPixmap(QPixmap(":/image/detail/cpuSystem.png"));
    systemLabel->resize(QPixmap(":/image/detail/cpuSystem.png").width() , QPixmap(":/image/detail/cpuSystem.png").height());
    waitLabel->setPixmap(QPixmap(":/image/detail/cpuWait.png"));
    waitLabel->resize(QPixmap(":/image/detail/cpuWait.png").width() , QPixmap(":/image/detail/cpuWait.png").height());
    idleLabel->setPixmap(QPixmap(":/image/detail/cpuIdle.png"));
    idleLabel->resize(QPixmap(":/image/detail/cpuIdle.png").width() , QPixmap(":/image/detail/cpuIdle.png").height());
    exitButton = new QPushButton(tr("BACK") ,exitLabel);
    exitButton->resize(100 , 40);
    exitButton->move(170 , 115);
    DockLayout->addWidget(exitLabel , 1 , 2 , Qt::AlignTop);
    DockLayout->addWidget(userLabel , 0 , 0 , Qt::AlignTop);
    DockLayout->addWidget(niceLabel , 0 , 1 , Qt::AlignTop);
    DockLayout->addWidget(systemLabel , 0 , 2 , Qt::AlignTop);
    DockLayout->addWidget(waitLabel , 1 , 0 , Qt::AlignTop);
    DockLayout->addWidget(idleLabel , 1 , 1 , Qt::AlignTop);
    this->setLayout(DockLayout);
    this->connect(exitButton , SIGNAL(clicked()) , this , SLOT(cpuDockClose()));
    cpuIndexUser = new DrawCurve(userLabel);
    cpuIndexNice = new DrawCurve(niceLabel);
    cpuIndexSystem = new DrawCurve(systemLabel);
    cpuIndexWait = new DrawCurve(waitLabel);
    cpuIndexIdle = new DrawCurve(idleLabel);
    cpuIndexUser->move(40 , 20);
    cpuIndexNice->move(40 , 20);
    cpuIndexSystem->move(40 , 20);
    cpuIndexWait->move(40 , 20);
    cpuIndexIdle->move(40 , 20);
    cpuIndexUser->nodeMinMax(userLabel , cpuInfo.user.max_node_name , cpuInfo.user.min_node_name);
    cpuIndexNice->nodeMinMax(niceLabel , cpuInfo.nice.max_node_name , cpuInfo.nice.min_node_name);
    cpuIndexSystem->nodeMinMax(systemLabel , cpuInfo.system.max_node_name , cpuInfo.system.min_node_name);
    cpuIndexWait->nodeMinMax(waitLabel , cpuInfo.wait.max_node_name , cpuInfo.wait.min_node_name);
    cpuIndexIdle->nodeMinMax(idleLabel , cpuInfo.idle.max_node_name , cpuInfo.idle.min_node_name);
    cpuIndexUser->cpuUserDraw = true;
    cpuIndexNice->cpuNiceDraw = true;
    cpuIndexSystem->cpuSystemDraw = true;
    cpuIndexWait->cpuWaitDraw = true;
    cpuIndexIdle->cpuIdleDraw = true;
}

void DockWindow::memDock()
{
    exitLabel = new QLabel(this);
    usedLabel = new QLabel(this);
    sharedLabel = new QLabel(this);
    cachedLabel = new QLabel(this);
    bufferedLabel = new QLabel(this);
    swappedLabel = new QLabel(this);
    exitLabel->setPixmap(QPixmap(":/image/detail/memExit.png"));
    exitLabel->resize(QPixmap(":/image/detail/memExit.png").width() , QPixmap(":/image/detail/memExit.png").height());
    usedLabel->setPixmap(QPixmap(":/image/detail/memUsed.png"));
    usedLabel->resize(QPixmap(":/image/detail/memUsed.png").width() , QPixmap(":/image/detail/memUsed.png").height());
    sharedLabel->setPixmap(QPixmap(":/image/detail/memShared.png"));
    sharedLabel->resize(QPixmap(":/image/detail/memShared.png").width() , QPixmap(":/image/detail/memShared.png").height());
    cachedLabel->setPixmap(QPixmap(":/image/detail/memCached.png"));
    cachedLabel->resize(QPixmap(":/image/detail/memCached.png").width() , QPixmap(":/image/detail/memCached.png").height());
    bufferedLabel->setPixmap(QPixmap(":/image/detail/memBuffered.png"));
    bufferedLabel->resize(QPixmap(":/image/detail/memBuffered.png").width() , QPixmap(":/image/detail/memBuffered.png").height());
    swappedLabel->setPixmap(QPixmap(":/image/detail/memSwapped.png"));
    swappedLabel->resize(QPixmap(":/image/detail/memSwapped.png").width() , QPixmap(":/image/detail/memSwapped.png").height());
    exitButton = new QPushButton(tr("BACK") ,exitLabel);
    exitButton->resize(100 , 40);
    exitButton->move(170 , 115);
    DockLayout->addWidget(exitLabel , 1 , 2 , Qt::AlignTop);
    DockLayout->addWidget(usedLabel , 0 , 0 , Qt::AlignTop);
    DockLayout->addWidget(sharedLabel , 0 , 1 , Qt::AlignTop);
    DockLayout->addWidget(cachedLabel , 0 , 2 , Qt::AlignTop);
    DockLayout->addWidget(bufferedLabel , 1 , 0 , Qt::AlignTop);
    DockLayout->addWidget(swappedLabel , 1 , 1 , Qt::AlignTop);
    this->setLayout(DockLayout);
    this->connect(exitButton , SIGNAL(clicked()) , this , SLOT(memoryDockClose()));
    memIndexUsed = new DrawCurve(usedLabel);
    memIndexShared = new DrawCurve(sharedLabel);
    memIndexCached = new DrawCurve(cachedLabel);
    memIndexBuffered = new DrawCurve(bufferedLabel);
    memIndexSwapped = new DrawCurve(swappedLabel);
    memIndexUsed->move(40 , 20);
    memIndexShared->move(40 , 20);
    memIndexCached->move(40 , 20);
    memIndexBuffered->move(40 , 20);
    memIndexSwapped->move(40 , 20);
    memIndexUsed->nodeMinMax(usedLabel , memoryInfo.used.max_node_name , memoryInfo.used.min_node_name);
    memIndexShared->nodeMinMax(sharedLabel , memoryInfo.shared.max_node_name , memoryInfo.shared.min_node_name);
    memIndexCached->nodeMinMax(cachedLabel , memoryInfo.cached.max_node_name , memoryInfo.cached.min_node_name);
    memIndexBuffered->nodeMinMax(bufferedLabel , memoryInfo.buffered.max_node_name , memoryInfo.buffered.min_node_name);
    memIndexSwapped->nodeMinMax(swappedLabel , memoryInfo.swapped.max_node_name , memoryInfo.swapped.min_node_name);
    memIndexUsed->memUsedDraw = true;
    memIndexShared->memSharedDraw = true;
    memIndexCached->memCachedDraw = true;
    memIndexBuffered->memBufferedDraw = true;
    memIndexSwapped->memSwappedDraw = true;
}

void DockWindow::flowDock()
{
    exitLabel = new QLabel(this);
    inLabel = new QLabel(this);
    outLabel = new QLabel(this);
    glueLabel = new QLabel(this);
    textLabel = new QLabel(glueLabel);
    glueLabel->setFixedSize(350 , 200);
    exitLabel->setPixmap(QPixmap(":/image/detail/flowExit.png"));
    exitLabel->resize(QPixmap(":/image/detail/flowExit.png").width() , QPixmap(":/image/detail/flowExit.png").height());
    inLabel->setPixmap(QPixmap(":/image/detail/flowIn.png"));
    inLabel->resize(QPixmap(":/image/detail/flowIn.png").width() , QPixmap(":/image/detail/flowIn.png").height());
    outLabel->setPixmap(QPixmap(":/image/detail/flowOut.png"));
    outLabel->resize(QPixmap(":/image/detail/flowOut.png").width() , QPixmap(":/image/detail/flowOut.png").height());
    textLabel->resize(300 , 150);
    textLabel->setWordWrap(true);
    textLabel->setFont(QFont("ZYSong18030" , 14));
    textLabel->setText("This page shows the traffic of the cluster. Usually reflect the network performance.");
    textLabel->move(50 , 0);
    exitButton = new QPushButton(tr("BACK") , exitLabel);
    exitButton->resize(100 , 40);
    exitButton->move(100 , 70);
    DockLayout->addWidget(exitLabel , 1 , 1 , Qt::AlignTop | Qt::AlignLeft);
    DockLayout->addWidget(inLabel , 0 , 0 , Qt::AlignBottom | Qt::AlignRight);
    DockLayout->addWidget(outLabel , 0 , 1 , Qt::AlignBottom | Qt::AlignLeft);
    DockLayout->addWidget(glueLabel , 1 , 0 , Qt::AlignTop | Qt::AlignRight);
    this->setLayout(DockLayout);
    this->connect(exitButton , SIGNAL(clicked()) , this , SLOT(flowDockClose()));
    flowIndexIn = new DrawCurve(inLabel);
    flowIndexOut = new DrawCurve(outLabel);
    flowIndexIn->move(40 , 20);
    flowIndexOut->move(40 , 20);
    flowIndexIn->nodeMinMax(inLabel , flowInfo.in.max_node_name , flowInfo.in.min_node_name);
    flowIndexOut->nodeMinMax(outLabel , flowInfo.out.max_node_name , flowInfo.out.min_node_name);
    flowIndexIn->flowInDraw = true;
    flowIndexOut->flowOutDraw = true;
}

void DockWindow::cpuDockClose()
{
    delete cpuIndexUser;
    delete cpuIndexNice;
    delete cpuIndexSystem;
    delete cpuIndexWait;
    delete cpuIndexIdle;
    delete exitButton;
    delete exitLabel;
    delete userLabel;
    delete niceLabel;
    delete systemLabel;
    delete waitLabel;
    delete idleLabel;
    this->close();
}

void DockWindow::memoryDockClose()
{
    delete memIndexUsed;
    delete memIndexShared;
    delete memIndexCached;
    delete memIndexBuffered;
    delete memIndexSwapped;
    delete exitButton;
    delete exitLabel;
    delete usedLabel;
    delete sharedLabel;
    delete cachedLabel;
    delete bufferedLabel;
    delete swappedLabel;
    this->close();
}

void DockWindow::flowDockClose()
{
    delete flowIndexIn;
    delete flowIndexOut;
    delete exitButton;
    delete exitLabel;
    delete inLabel;
    delete outLabel;
    this->close();
}
